import axios from 'axios';

import Config from '../../config';
import Web3Manager from '../../libs/web3-manager';
import { AuthenticationType } from './authentication';

export default class AuthenticationManager {
    static KEY = 'authentication-key';
    static SECRET = 'authentication-secret';

    static Endpoint = {
        CHECK: '/authentication/check'
    };

    static isValid = async (): Promise<AuthenticationType> => {
        try {
            const call = await axios.post(
                Config.getAPI() + AuthenticationManager.Endpoint.CHECK,
                {},
                {
                    headers: {
                        [AuthenticationManager.KEY]: AuthenticationManager.getKey(),
                        [AuthenticationManager.SECRET]: AuthenticationManager.getSecret(),
                        wallet_address: await Web3Manager.getWalletAddress()
                    }
                }
            );
            return call.data['status'];
        } catch (exception) {
            return AuthenticationType.NOT_AUTHORIZED;
        }
    };

    static getKey = (): string => {
        return window.localStorage.getItem(AuthenticationManager.KEY);
    };

    static setKey = (key: string) => {
        window.localStorage.setItem(AuthenticationManager.KEY, key);
    };

    static getSecret = (): string => {
        return window.localStorage.getItem(AuthenticationManager.SECRET);
    };

    static setSecret = (secret: string) => {
        window.localStorage.setItem(AuthenticationManager.SECRET, secret);
    };
}
